<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: gavin
 * Date: 15-5-4
 * Time: 下午12:35
 */

class Xueme extends Foreground_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('captcha');
        $this->load->library('pagination');
        $this->load->model('ghc_options_model');
        $this->load->model('xueme_user_model');

    }

    public function index()
    {
        echo "this is index";
    }

    public function show($content="")
    {
        echo "this content".$content;
    }

    public function signin()
    {
        $this->load->view("signin.html");
    }
    public function signup()
    {
        $this->load->view("signup.html");
    }

    public function logincheck()
    {
        //验证码验证
        /*$code = strtolower($this->session->userdata('code'));
        $getcode = strtolower($this->input->post('captcha'));
        if($code != $getcode)
        {
            header("location:".SITE_URL.'index.php/admin/login');
            exit;
        }*/
        //设置验证规则
        $this->form_validation->set_rules('username','username','trim|required|xss_clean');
        $this->form_validation->set_rules('password','password','required|xss_clean');
        //$this->form_validation->set_rules('password','password','required|md5|xss_clean');
        if($this->form_validation->run()  == TRUE)
        {//通过表单验证

            $username = $this->input->get_post('username');
            $password = $this->input->get_post('password');

            //进行数据库用户验证
            $result = $this->xueme_user_model->existUser($username,$password);

            if(count($result) == 1)
            {//存在用户  存储用户信息到session 用户uid 角色
                $this->session->set_userdata('userid',$result[0]->id);
                $this->session->set_userdata('username',$username);
                //echo "session OK";
                echo "OK";
                exit;
            }
            else{//不存在该用户
                echo "user not find";
                //$log = sprintf("attempt login, username:%s, password:%s, IP:%s, UserAgent:%s.",$username,$seepassword,$this->input->ip_address(),$this->input->user_agent());
                //log_message('error', $log);
                header("location:".SITE_URL.'index.php/xueme/signin');
                exit;
            }

        }
        else
        {//input is not legal
            echo "input is not legal";
            header("location:".SITE_URL.'index.php/xueme/signin');
            exit;
        }
    }

    /**
     * @return bool
     * 判断是否存在username 和 role(角色)
     */
    private function is_Session()
    {
        $userid = $this->session->userdata('userid');
        $value = $this->session->userdata('username');
        if($value && $userid)
        {
            return TRUE;
        }
        else
        {
            //$log = sprintf("no login visit,  IP:%s, UserAgent:%s.",$this->input->ip_address(),$this->input->user_agent());
            //log_message('error', $log);
            return FALSE;
        }
    }
}
